<?php

class FilesClass {
	function FilesClass () {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function file_get_link($id) {
		$id = intval($id);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE `id` = ".$id." AND `types`='binfile'", __LINE__, __FILE__);
		$row = mysql_num_rows($sql);
		if ($row != 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$row = unserialize($row['info']);
			return $row['name'];
		}
	}

	function file_add($inf, $name, $link, $parent, $usr, $rewrite=0) {
		$dat = date("YmdHis");
		$info['name'] = $link;
		$info['type'] = $inf['type'];
		$info['size'] = $inf['size'];
		$info = serialize($info);
		$name = addslashes($name);
		if ($rewrite != 0) {
			$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."files` where parent = '".$parent."' and types = 'binfile'", __LINE__, __FILE__);
			$row = mysql_num_rows($sql);
			if ($row != 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$this->file_del($row['id']);
			}
		}		
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."files` (`id`,`parent`,`name`,`info`,`save_date`,`owner`, `types`)	VALUES ('', '$parent', '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($info)."', '$dat', '$usr', 'binfile')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		$i = mysql_insert_id();
		return $i;
	}

	function file_del_with_parent_data($parent, $name, $image_id){
		global $treeclass;
		$treeclass->mod_item_data($parent, $name, "");
		//$this->file_del($image_id);
	}
	
	function file_del($id) {
		global $setupdata;
		$id = intval($id);
		if ($id > 0) {
			$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
			$row = mysql_num_rows($sql);
			if ($row != 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$parent = $row['parent'];
				$row = unserialize($row['info']);
				$files = $setupdata['binary_path']."/".$row['name'];
				if (!(file_exists($files))) {
					$setupdata['binary_path'] = $setupdata['binary_path']."/".$parent;
					$files = $setupdata['binary_path']."/".$row['name'];
				}
				if ((strlen($files) > 0) and (file_exists($files)) and (strlen($row['name']) > 0)){
					list($par, $d, $s) = explode("-", $row['name']);
					$pattern = $par."-".$d;
					if (is_dir($setupdata['binary_path'])){
						$d = opendir($setupdata['binary_path']);
						while ($file = readdir($d)){
							if (is_file($setupdata['binary_path']."/".$file)) {
								if (strpos($file, $pattern) === FALSE){
								} else{
									unlink($setupdata['binary_path']."/".$file);
								}
							}
						}
						closedir($d);
					}
				}
				if (file_exists($files) === FALSE ){
					$this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."files` where ".$id."=`id`", __LINE__, __FILE__);
				}
			}
		}
	}

	function file_sel($id, $paging=false) {
		$rows = array();
		$id = intval($id);
		if ($paging === false){
			if ($id > 0) $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE parent=".$id." and types = 'binfile' ORDER BY `save_date` DESC";
			else $query="select * FROM `".$this->setupdata['table_prefix']."files` WHERE types = 'binfile' ORDER BY `save_date` DESC";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				array_push($rows, $row);
			}
			return $rows;
		} else {
			$from = $paging['curent_page'] * $paging['items_per_page'];
			if ($id > 0) $str = " parent=".$id." and ";
			else $str = "";
			$query="SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'binfile' ORDER BY `save_date` DESC LIMIT ".$from.", ".$paging['items_per_page'];
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$row['info'] = unserialize($row['info']);
				$rows['list'][] =  $row;
			}
			$query="SELECT COUNT(`id`) FROM `".$this->setupdata['table_prefix']."files` WHERE ".$str."types = 'binfile'";
			$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
			$row = mysql_fetch_array($sql);
			$rows['count'] = $row[0];
			return $rows;
		}
	}
	
	function file_info($id){
		$id = intval($id);
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
		$row = mysql_num_rows($sql);
		if ($row != 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$rows['parent'] = $row['parent'];
			$row = unserialize($row['info']);
			$rows['link'] = $row['name'];
			$rows['size'] = $row['size'];
			$rows['mime'] = $row['type'];
			return $rows;
		}
	}
	
	function files_name_mod($id, $name, $usr){
		$id = intval($id);
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."files` where id = '$id'", __LINE__, __FILE__);
		$row = mysql_num_rows($sql);
		if ($row == 1) {
			$dat = date("YmdHis");
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."files` SET name='".$name."', save_date ='".$dat."', owner ='".$usr."' WHERE id = ".$id, __LINE__, __FILE__);
		}
	}
	
	function file_real_name($link){
		list($s, $s, $s, $name) = explode("-", $link);
		$name = strstr($link, $name);
		return $name;
	}
}
?>
